// pages/books-detail/index.js
import {handleQuickLogin} from '../../utils/tools'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        isDesk:false,   //是否已被加入书桌 
        deskid:'',  //记录待删除的书桌id
        isShow:false  //控制底部按钮容器显示
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //渲染详情
        wx.getStorage({
            key:'detail',
            success:({data})=>{
                console.log(data)
                this.checkDesk(data.objectId)
                this.setData(data)
                let uuid = data.sourceUrl.split('/source/')[1]
                let url = `https://m.yuedu.163.com/reader/book/info.json?source_uuid=${uuid}&catalog_only=true`
                wx.request({
                  url,
                  success:(res)=>{
                      console.log(res);
                      let {catalog} = res.data.data
                      this.setData({
                          catalog
                      })
                  }
                })
            }
        })
    },
    checkDesk(bookid){ //初始化书桌状态
        let {userInfo} = getApp().globalData
        if(userInfo){
            getApp().fly.get('classes/MiniDesk',{
                where:{
                    bookid,
                    userid:userInfo.userid
                }
            }).then(res=>{
                if(res.results.length){
                    this.setData({
                        isDesk:true,
                        deskid:res.results[0].objectId
                    })
                }
                this.setData({isShow:true})
            })
        }else{
            this.setData({isShow:true})
        }
    },
    onChange(ev){
        console.log(ev);
        let {index} = ev.detail
        this.setData({
            active:index
        })
    },
    handleDeskAdd(){ //加入书桌
        let {userInfo} = getApp().globalData
        if(!userInfo){
            wx.showModal({
              title:'登录后才能操作',
              content:'是否授权一键登录',
              success:({confirm})=>{
                if(confirm){
                    handleQuickLogin(()=>{
                        this.checkDesk(this.data.objectId)
                    })
                    
                }
              }
            })
            console.log('未登录');
            return
        }
        console.log('加入书桌');
        let {userid} = userInfo
        let bookid = this.data.objectId
        let {iconUrl,title} = this.data
        getApp().fly.post('classes/MiniDesk',{
            userid,bookid,iconUrl,title
        }).then(res=>{
            wx.showToast({
              title: '加入成功',
              icon:'none'
            })
            this.setData({
                isDesk:true,
                deskid:res.objectId //加入成功后记录书桌id
            })
        })
    },
    handleDeskDel(){ //移出书桌
        let {deskid} = this.data
        getApp().fly.delete(`classes/MiniDesk/${deskid}`).then(res=>{
            wx.showToast({
              title: '移出成功',
              icon:'none'
            })
            this.setData({
                isDesk:false
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})